import Constants from "./Constants";
import Helper from "./HelperMgr";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GameData {
    

    private static _instance: GameData = null;
    public static get instance() {
        if (!GameData._instance) { new GameData() };
        return GameData._instance;
    }
    constructor() { this.init(); GameData._instance = this; }


    //全局数据 ： 保存

    /**游戏关卡 */
    gameStage: number = 1;
    /**游戏解锁最大关卡 */
    gameMaxStage:number = 1;
    /**玩家正常模式游戏分数 */
    playScores: Array<number> = []
     /**玩家疯狂游戏分数 */
     playCrazyScores: Array<number> = []
    /**玩家当前分数 */
    playScore: number = 0
    /**玩家当前棒子 */
    playRod: number = 0
    /**玩家当前鱼饵 */
    playStosh: number = 1
    /**玩家当前拥有棒子索引 */
    playRods: Array<number> = []
    /**玩家当前拥有鱼饵索引 */
    playStoshs: Array<number> = []
    /**玩家当前金币 */
    playGold: number = 0
    /**签到的日期 */
    signDays: Array<number> = [];
    /**今日是否签到 */
    isSignedToday : boolean = false;
    /**商店兑换物品种类 */
    shopIsStosh:boolean = false;
    /**商店兑换物品索引 */
    shopIndex : number = 0;

    //临时数据 ： 不保存
     /**游戏模式 true:正常模式,false:乱打模式 */
     gameModel: boolean =  true;
     /**玩家是否点击了返回主页 */
     gameBack:boolean = false;
     gameScore:number = 0;
     gameChance:number = 0;
  


    init() {
        this.loadData();
    }

  
    loadData() {
        let day = (new Date()).getDate()

        this.gameStage = JSON.parse(localStorage.getItem("gameStage") || '1');
        this.gameMaxStage = JSON.parse(localStorage.getItem("gameMaxStage") || '1');
        this.playScores = JSON.parse(localStorage.getItem("playScores") || '[0,0,0,0,0]');
        this.playCrazyScores = JSON.parse(localStorage.getItem("playCrazyScores") || '[0,0,0,0,0]');
        this.playRod = JSON.parse(localStorage.getItem("playRod") || '0');
        this.playStosh = JSON.parse(localStorage.getItem("playStosh") || '0');
        this.playGold = JSON.parse(localStorage.getItem("playGold") || '0');
        this.playRods = JSON.parse(localStorage.getItem("playRods") || '[0,0,0]');
        this.playStoshs = JSON.parse(localStorage.getItem("playStoshs") || '[0,0,0]');

        let sinDaysStr = localStorage.getItem("signDays") || '';
        if (sinDaysStr.length > 0) {
            this.signDays = Helper.splitToNumber(sinDaysStr, ',');
        }
        if (this.signDays.length >= 7) {
            this.signDays = [];
            localStorage.setItem("signDays", "")
        }
        if(sinDaysStr.length <= 0){
            this.signDays.push(day);
            this.playGold += Constants.SignReward[0];
        }
        this.isSignedToday = this.signDays.indexOf(day) > -1;

    }

    


    saveData() {
        if(this.gameStage > this.gameMaxStage){
            this.gameMaxStage = this.gameStage;
        }
        localStorage.setItem("gameStage", this.gameStage.toString());
        localStorage.setItem("gameMaxStage", this.gameMaxStage.toString())
        localStorage.setItem("playRod", this.playRod.toString());
        localStorage.setItem("playGold", this.playGold.toString());
        localStorage.setItem("playStosh", this.playStosh.toString());
        localStorage.setItem("playScores", JSON.stringify(this.playScores))
        localStorage.setItem("playCrazyScores", JSON.stringify(this.playCrazyScores))
        localStorage.setItem("playRods", JSON.stringify(this.playRods))
        localStorage.setItem("playStoshs", JSON.stringify(this.playStoshs))
        localStorage.setItem("signDays",this.signDays.join(','));


    }
}
